package ysq.xyg.sys.controller;

import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysDepart;
import ysq.xyg.sys.model.po.SysRole;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysRoleVo;
import ysq.xyg.sys.model.vo.SysUserVo;
import ysq.xyg.sys.service.SysDepartService;
import ysq.xyg.sys.service.SysRoleService;
import ysq.xyg.sys.service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 系统管理-用户信息controller层
 *
 * @author shengQiang yu
 * @date 2019-3-30 11:24:36
 */
@RestController
public class SysUserController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysUserController.class);
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private SysDepartService sysDepartService;
    private SysUser sysUser;
    /**
     * 分享页面
     *
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @RequestMapping(value = "/sys/sysUserListShare",method = RequestMethod.GET)
    public ModelAndView sysUserListShare(HttpServletRequest request){
        ModelAndView view = new ModelAndView("/sys/sysUserListShare");
        SysUser currentUser=acceptCurrentUser(request);
        view.addObject("currentUser", currentUser);
        return view;
    }
    /**
     * 分页推广查询
     *
     * @param sysUserVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/sharePage",method = RequestMethod.POST)
    public ReturnEntity sharePage(HttpServletRequest request,SysUserVo sysUserVo, int pageSize, int pageNum){
        SysUser currentUser=acceptCurrentUser(request);
        if(sysUserVo==null){
            sysUserVo=new SysUserVo();
        }
        sysUserVo.setOneOrTwoIntroduce(currentUser.getId());
        ReturnEntity returnEntity = sysUserService.pageLeft(sysUserVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 分页查询
     *
     * @param sysUserVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/page",method = RequestMethod.POST)
    public ReturnEntity page(SysUserVo sysUserVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysUserService.pageLeft(sysUserVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 个人资料
     *
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @RequestMapping(value = "/sysUser/myNews",method = RequestMethod.GET)
    public ModelAndView getMyEntityNews(HttpServletRequest request){
        SysUser currentUser=acceptCurrentUser(request);
        currentUser=sysUserService.selectByPrimaryKey(currentUser.getId());
        ModelAndView view = new ModelAndView("/sys/sysUserNews");
        view.addObject("sysDepart", sysDepartService.selectByPrimaryKey(currentUser.getDepartId()));
        view.addObject("sysRole", sysRoleService.selectByPrimaryKey(currentUser.getRoleId()));
        view.addObject("currentUser", currentUser);
        return view;
    }
    /**
     * 获取组织机构和人员树
     *
     * @param sysUserVo 对象
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理", className = "用户信息", methods = "组织机构和人员树", businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE, operatorType = BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/dTreeList")
    public ReturnEntity dTreeList(SysUserVo sysUserVo) {
        return sysUserService.dTreeList(sysUserVo);
    }
    /**
     * 推广人员修改
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @RequestMapping(value = "/sysUser/getEntityShare",method = RequestMethod.GET)
    public ModelAndView getEntityShare(HttpServletRequest request,String id){
        ModelAndView view = new ModelAndView("/sys/sysUserUpdateOrSaveShare");
        view.addObject("currentUser", acceptCurrentUser(request));
        if(StrUtil.isNotEmpty(id)){
            sysUser = sysUserService.selectByPrimaryKey(id);
            if(sysUser!=null){
                SysDepart sysDepart=sysDepartService.selectByPrimaryKey(sysUser.getDepartId());
                if(sysDepart!=null){
                    sysUser.setDepartName(sysDepart.getName());
                }
            }
            view.addObject("sysUser", sysUser);
        }
        List<SysRole> list=sysRoleService.acceptList(new SysRoleVo(BaseConstant.BASE_STATUS_NORMAL));
        view.addObject("list", list);
        return view;
    }
    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @RequestMapping(value = "/sysUser/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request,String id){
        SysUser currentUser=acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/sys/sysUserUpdateOrSave");
        view.addObject("currentUser", currentUser);
        if(StrUtil.isNotEmpty(id)){
            sysUser = sysUserService.selectByPrimaryKey(id);
            if(sysUser!=null){
               SysDepart sysDepart=sysDepartService.selectByPrimaryKey(sysUser.getDepartId());
               if(sysDepart!=null){
                   sysUser.setDepartName(sysDepart.getName());
               }
            }
            view.addObject("sysUser", sysUser);
        }
        List<SysRole> list=sysRoleService.acceptList(new SysRoleVo(BaseConstant.BASE_STATUS_NORMAL));
        view.addObject("list", list);
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return sysUserService.delete(id,acceptCurrentUser(request));
    }
    /**
     * 添加下级
     *
     * @param sysUserVo Vo对象
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "添加下级",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/addShare",method = RequestMethod.POST)
    public ReturnEntity addShare(HttpServletRequest request,SysUserVo sysUserVo){
        return sysUserService.addShare(sysUserVo,acceptCurrentUser(request));
    }
    /**
     * 添加或修改
     *
     * @param sysUserVo Vo对象
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity addOrUpdate(HttpServletRequest request,SysUserVo sysUserVo){
        return sysUserService.addOrUpdate(sysUserVo,acceptCurrentUser(request));
    }
    /**
     * 修改个人信息
     *
     * @param sysUserVo 对象
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "修改个人信息",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/updateNews",method = RequestMethod.POST)
    public ReturnEntity updateNews(HttpServletRequest request,SysUserVo sysUserVo){
        return sysUserService.updateNews(request,sysUserVo,acceptCurrentUser(request));
    }
    /**
     * 重置密码
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-3-30 11:24:36
     */
    @BaseLog(module = "系统管理",className = "用户信息",methods = "重置密码",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysUser/resetPassword")
    public ReturnEntity resetPassword(HttpServletRequest request,String id){
        return sysUserService.resetPassword(id,acceptCurrentUser(request));
    }
}